package com.blb.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.ssm.entity.Role;
import com.blb.ssm.entity.User;
import com.blb.ssm.entity.UserRole;
import com.blb.ssm.mapper.UserMapper;
import com.blb.ssm.service.IUserRoleService;
import com.blb.ssm.service.IUserService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huahao
 * @since 2019-01-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
	
	@Autowired
	private IUserRoleService userRoleService;
	
	@Override
	public User getUserByUserName(String userName) {
		return getOne(new QueryWrapper<>(new User()).eq("user_name", userName));
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void setRole(String id, List<Role> roles) {
		userRoleService.remove(new QueryWrapper<>(new UserRole()).eq("user_id", id));
		
		for (Role role : roles) {
			UserRole userrole = new UserRole();
			userrole.setUserId(id);
			userrole.setRoleId(role.getId());
			userRoleService.save(userrole);
		}
	}
}
